#include "SPI.h" // SPI library
#include "MFRC522.h" // RFID library

const int buzzer = 8;//蜂鸣器的控制引脚数字8
const int pinRST = 9;
const int pinSDA = 10;
const int RED = 2;
const int Green = 3;

MFRC522 mfrc522(pinSDA, pinRST); // Set up mfrc522 on the Arduino


void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer,OUTPUT);
  pinMode(RED,OUTPUT);
  pinMode(Green,OUTPUT);
  SPI.begin(); // open SPI connection
  mfrc522.PCD_Init(); // Initialize Proximity Coupling Device (PCD)
  Serial.begin(9600); // open serial connection
}

void loop() {
  // put your main code here, to run repeatedly:

  unsigned char i,j,z;
  String content= "";

  
  
  if (!mfrc522.PICC_IsNewCardPresent()) // (true, if RFID tag/card is present ) PICC = Proximity Integrated Circuit Card
  {
    return;
    }
  if(!mfrc522.PICC_ReadCardSerial()) // true, if RFID tag/card was read
  {
    return;
    }
      
  //串口打印RTID ID         
  Serial.print("RFID TAG ID:");      
  for (byte i = 0; i < mfrc522.uid.size; ++i) { // read id (in parts)
    Serial.print(mfrc522.uid.uidByte[i], HEX); // print id as hex values
    Serial.print(" "); // add space between hex blocks to increase readability
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));       
    }                
   Serial.println(); // Print out of id is complete.

   
  //判断是不是已经录入系统的ID卡
   content.toUpperCase();
   if ((content.substring(1) == "00 C6 20 20")) //change here the UID of the card/cards that you want to give access
    {
      Serial.println("Authorized access");
      Serial.println();
      digitalWrite(Green, HIGH);
      delay(1);
      digitalWrite(Green, LOW);
        
      for(j=0;j<80;j++){
        digitalWrite(buzzer,HIGH);
        delay(1);
        digitalWrite(buzzer,LOW);
        delay(1);
        }
               
    }
    else{  
    Serial.println(" Access denied");
    digitalWrite(RED, HIGH);
    delay(1);
    digitalWrite(RED, LOW);

    for(z=0;z<100;z++){
        digitalWrite(buzzer,HIGH);
        delay(2);
        digitalWrite(buzzer,LOW);
        delay(2);
        }
    
  }
   delay(1000);    
   
}
